local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

--概述：控制怪物的动画播放
----------
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

	self.player=self.parent.player
	self.character=self.parent

    self.TagAtt=self.parent:GetTagAtt()
    self.Humanoid=self.character:GetHumanoid()

	self.TagCharacter=self.player.character
	self.TagHumanoid=self.TagCharacter:GetHumanoid()

	

	self.IdleState=self.character:GetState("idle")
	self.FollowState=self.character:GetState("follow")
	self.AttackState=self.character:GetState("attack")


	self.IdleState.EnterEvent:Connect(function()
		self.State=1
	end)

	self.FollowState.EnterEvent:Connect(function()
		self.State=2
	end)
	self.AttackState.EnterEvent:Connect(function()
		self.State=3
	end)

	self.IdleState.ExitEvent:Connect(function()
		self.State=nil
	end)

	self.FollowState.ExitEvent:Connect(function()
		self.State=nil
	end)
	self.AttackState.ExitEvent:Connect(function()
		self.State=nil
	end)
	

	self.UpdateCon=self:AddStepUpdate(function()
		self:Update()
	end)


end;


function M.dtor(self)
	if self.UpdateCon then
		self.UpdateCon:Disconnect()
		self.UpdateCon=nil
	end
	M.super.dtor(self);
end

function M.GetDistance2TagCharacter(self)
	local tagPos=self.TagCharacter:GetPosition()
	return (self.character:GetPosition()-tagPos).Magnitude
end


function M.GetDistance2TagPos(self)
	return (self.character:GetPosition()-self.TagAtt.WorldCFrame.Position).Magnitude
end

function M.Update(self)

	if self.State==1 or self.State==3 then 
		local ds=self:GetDistance2TagPos()
		if ds>2 then --超出2米切换到跟随状态
			self.character:SwitchState("follow")
		end
	elseif self.State==3 then --攻击状态切换到跟随，距离更大
		local ds=self:GetDistance2TagPos()
		if ds>6 then --超出6米切换到跟随状态
			self.character:SwitchState("follow")
		end

	elseif self.State==2 then

		self.Humanoid.WalkSpeed=self.TagHumanoid.WalkSpeed

		local ds=self:GetDistance2TagCharacter()

	
		if ds>50 then --跟玩家距离超出60，直接移动到玩家位置

			self.character:SetPosition(self.TagCharacter:GetPosition())

		elseif self:GetDistance2TagPos()<2.5 then --小于1米，回到idle状态
			self.character:SwitchState("idle")
		else
			self.Humanoid:MoveTo(self.TagAtt.WorldCFrame.Position)
		end
	elseif self.State==3 then

	end
end


return M